﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections.Generic;
using Database.Classes;

public partial class UserDefinedFunctions
{
    public static double GetAvgProduction()
    {
        try
        {
            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT AVG(Factories + PopulationLevel) AS Expr1 FROM Planets WHERE (OwnerID > 1)", conn))
                {
                    return double.Parse(cmd.ExecuteScalar().ToString());
                }
            }
        }
        catch (Exception)
        {
            return 0;
        }
    }
    public static double GetAVG(string science)
    {
        try
        {
            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Select AVG(" + science + ") From Players Where PlayerId > 1", conn))
                {
                    return double.Parse(cmd.ExecuteScalar().ToString());
                }
            }
        }
        catch (Exception)
        {
            return 0;
        }
    }
    public static Dictionary<string, double> AvgSciences()
    {
        Dictionary<string, double> tmp = new Dictionary<string, double>();
        tmp.Add("Biology", GetAVG("Biology"));
        tmp.Add("Economy", GetAVG("Economy"));
        tmp.Add("Energy", GetAVG("Energy"));
        tmp.Add("Math", GetAVG("Math"));
        tmp.Add("Physics", GetAVG("Physics"));
        tmp.Add("Social", GetAVG("Social"));
        tmp.Add("Culture", GetAVG("CurrentCulture"));
        return tmp;
    }
};

